package actual3d;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluPerspective;

import java.util.Random;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.*;
import org.lwjgl.*;

public class Attempt1
{
	/*********************************** BEGIN CONSTANTS *******************************************/
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	public static final int ZNEAR = 1;

	/*********************************** END CONSTANTS *******************************************/

	public Attempt1()
	{
		try
		{
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.setTitle("Hello, LWJGL!");
			Display.create();
		} catch (LWJGLException e)
		{
			e.printStackTrace();
		}

		// Initialize OpenGL
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective((float) 30, 640f / 480f, 0.001f, 100);
		glMatrixMode(GL_MODELVIEW);

		Point[] points = new Point[10000];
		Random random = new Random();

		for (int i = 0; i < points.length; i++)
		{
			points[i] = new Point((random.nextFloat() - 0.5f) * 100f, (random.nextFloat() - 0.5f) * 100f, random.nextInt(200) - 200);
		}

		float speed = 0.0f;
		float xspeed = 0.0f;

		while (!Display.isCloseRequested())
		{
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			glTranslatef(xspeed, 0, speed);

			glBegin(GL_POINTS);
			for (Point p : points)
			{
				glVertex3f(p.x, p.y, p.z);
			}
			glEnd();

			while (Keyboard.next())
			{
				if (Keyboard.isKeyDown(Keyboard.KEY_W))
					speed = 0.5f;
				else if (Keyboard.isKeyDown(Keyboard.KEY_S))
					speed = -0.5f;
				else 
					speed = 0.0f;
				
				if (Keyboard.isKeyDown(Keyboard.KEY_A))
					xspeed = 0.5f;
				else if (Keyboard.isKeyDown(Keyboard.KEY_D))
					xspeed = -0.5f;
				else
					xspeed = 0.0f;
				
				if (Keyboard.isKeyDown(Keyboard.KEY_SPACE))
					speed = 0.0f;
				if (Keyboard.isKeyDown(Keyboard.KEY_C))
				{
					speed = 0.0f;
					glLoadIdentity();
				}
			}

			Display.update();
			Display.sync(60);
		}
		Display.destroy();
	}

	private static class Point
	{
		float x, y, z;

		public Point(float x, float y, float z)
		{
			this.x = x;
			this.y = y;
			this.z = z;
		}
	}

	public static void main(String[] args)
	{
		new Attempt1();

	}

	private int GetMouseDY()
	{
		return -Mouse.getDY();
	}

	private int GetMouseY()
	{
		return HEIGHT - Mouse.getY() - 1;
	}

}
